package com.zhang.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.common.domain.IdInfo;
import com.zhang.common.mapper.read.IdInfoMapper;
import com.zhang.common.service.IdInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * IdInfoServiceImpl
 *
 * @author zhangxin
 * @date 2021/7/5
 */
@Service
public class IdInfoServiceImpl  extends ServiceImpl<IdInfoMapper, IdInfo> implements IdInfoService{
    @Autowired
    private IdInfoMapper idInfoMapper;

    public IdInfoServiceImpl() {
    }

    @Transactional(
            propagation = Propagation.REQUIRES_NEW,
            rollbackFor = {Exception.class}
    )
    public IdInfo findAndUpdateIdInfo(String idType) {
        IdInfo info = (IdInfo)this.idInfoMapper.selectOneByIdType(idType);
        Assert.notNull(info, String.format("idType:%s 流水号表中未配置!", idType));
        long count = this.idInfoMapper.updateMaxId(idType);
        Assert.isTrue(count == 1L, String.format("idType:%s 流水号表更新失败!", idType));
        return info;
    }
}

